<?php

/**
 * Date: 2015-10-09
 */

namespace Admin\Controller;

use Admin\Logic\GoodsLogic;

class SystemController extends BaseController {
    /*
     * 配置入口
     */

    public function index() {
        /* 配置列表 */
        //$group_list = array('shop_info' => '商城信息', 'basic' => '产品设置', 'sms' => '短信设置', 'shopping' => '购物流程设置', 'smtp' => '邮件设置', 'water' => '水印设置', 'distribut' => '分销设置', 'web_info' => '系统设置', 'reg' => '注册设置', 'shares' => '股票设置', 'cash' => '奖金设置', 'reg' => '注册设置');
        $group_list = array('shop_info' => '商城信息', 'shopping' => '财务设置', 'basic' => '产品设置', 'water' => '水印设置', 'reg' => '注册设置', 'smtp' => '邮件设置', 'sms' => '短信设置','zfuwl' => '初始设置');
        $this->assign('group_list', $group_list);
        $inc_type = I('get.inc_type', 'shop_info');
        $this->assign('inc_type', $inc_type);
         $config = tpCache($inc_type);
        if($inc_type == 'shopping'){
            $config['tk_time'] = objarray_to_array(json_decode($config['tk_time']));
        }
        $this->assign('config', $config); //当前配置项
        C('TOKEN_ON', false);
        $this->display($inc_type);
    }

    /*
     * 新增修改配置
     */

    public function handle() {
        $param = I('post.');
        $inc_type = $param['inc_type'];
         if($inc_type == 'shopping'){
            $param['tk_time'] = json_encode($param['tk_time']);
        }
        unset($param['inc_type']);
        tpCache($inc_type, $param);
        $this->success("操作成功", U('System/index', array('inc_type' => $inc_type)));
    }

    /**
     * 自定义导航
     */
    public function navigationList() {
        $model = M("Navigation");
        $navigationList = $model->order("id desc")->select();
        $this->assign('navigationList', $navigationList);
        $this->display('navigationList');
    }

    /**
     * 添加修改编辑 前台导航
     */
    public function addEditNav() {
        $model = D("Navigation");
        if (IS_POST) {
            $model->create();
            if ($_GET['id'])
                $model->save();
            else
                $model->add();

            $this->success("操作成功!!!", U('Admin/System/navigationList'));
            exit;
        }
        // 点击过来编辑时
        $id = $_GET['id'] ? $_GET['id'] : 0;
        $navigation = $model->find($id);

        // 系统菜单
        $GoodsLogic = new GoodsLogic();
        $cat_list = $GoodsLogic->goods_cat_list();
        $select_option = array();
        foreach ($cat_list AS $key => $value) {
            $strpad_count = $value['level'] * 4;
            $select_val = U("/Home/Goods/goodsList", array('id' => $key));
            $select_option[$select_val] = str_pad('', $strpad_count, "-", STR_PAD_LEFT) . $value['name'];
        }
        $system_nav = array(
            '/index.php?m=Home&c=Index&a=promoteList' => '限时抢购',
            '/index.php?m=Home&c=Activity&a=group_list' => '团购',
            '/index.php?m=Home&c=Goods&a=integralMall' => '积分商城',
            '/index.php?m=Home&c=Article&a=index' => '新闻公告',
            '/index.php?m=Home&c=Bank&a=money_day_log' => '财务明细',
            '/index.php?m=Home&c=Zmd&a=tjr_list' => '团队管理',
            '/index.php?m=Home&c=User&a=info' => '账户管理',
        );
//        $system_nav = array_merge($system_nav, $select_option);
        $this->assign('system_nav', $system_nav);

        $this->assign('navigation', $navigation);
        $this->display('_navigation');
    }

    /**
     * 删除前台 自定义 导航
     */
    public function delNav() {
        // 删除导航
        M('Navigation')->where("id = {$_GET['id']}")->delete();
        $this->success("操作成功!!!", U('Admin/System/navigationList'));
    }

    public function refreshMenu() {
        $pmenu = $arr = array();
        $rs = M('system_module')->where('level>1 AND visible=1')->order('mod_id ASC')->select();
        foreach ($rs as $row) {
            if ($row['level'] == 2) {
                $pmenu[$row['mod_id']] = $row['title']; //父菜单
            }
        }

        foreach ($rs as $val) {
            if ($row['level'] == 2) {
                $arr[$val['mod_id']] = $val['title'];
            }
            if ($row['level'] == 3) {
                $arr[$val['mod_id']] = $pmenu[$val['parent_id']] . '/' . $val['title'];
            }
        }
        return $arr;
    }

    /**
     * 清空系统缓存
     */
    public function cleanCache() {
        delFile('./Application/Runtime'); // 清除所有
        $this->error("操作完成!!!");
    }

    /**
     * 清空静态商品页面缓存
     */
    public function ClearGoodsHtml() {
        $goods_id = I('goods_id');
        if (unlink("./Application/Runtime/Html/Home_Goods_goodsInfo_{$goods_id}.html")) {
            // 删除静态文件
            $html_arr = glob("./Application/Runtime/Html/Home_Goods*.html");
            foreach ($html_arr as $key => $val) {
                strstr($val, "Home_Goods_ajax_consult_{$goods_id}") && unlink($val); // 商品咨询缓存
                strstr($val, "Home_Goods_ajaxComment_{$goods_id}") && unlink($val); // 商品评论缓存
            }
            $json_arr = array('status' => 1, 'msg' => '清除成功', 'result' => '');
        } else {
            $json_arr = array('status' => -1, 'msg' => '未能清除缓存', 'result' => '');
        }
        $json_str = json_encode($json_arr);
        exit($json_str);
    }

    /**
     * 商品静态页面缓存清理
     */
    public function ClearGoodsThumb() {
        $goods_id = I('goods_id');
        delFile("./Public/upload/goods/thumb/$goods_id"); // 删除缩略图
        $json_arr = array('status' => 1, 'msg' => '清除成功,请清除对应的静态页面', 'result' => '');
        $json_str = json_encode($json_arr);
        exit($json_str);
    }

    /**
     * 清空 文章静态页面缓存
     */
    public function ClearAritcleHtml() {
        $article_id = I('article_id');
        unlink("./Application/Runtime/Html/Index_Article_detail_{$article_id}.html"); // 清除文章静态缓存
        unlink("./Application/Runtime/Html/Doc_Index_article_{$article_id}_api.html"); // 清除文章静态缓存
        unlink("./Application/Runtime/Html/Doc_Index_article_{$article_id}_phper.html"); // 清除文章静态缓存
        unlink("./Application/Runtime/Html/Doc_Index_article_{$article_id}_android.html"); // 清除文章静态缓存
        unlink("./Application/Runtime/Html/Doc_Index_article_{$article_id}_ios.html"); // 清除文章静态缓存
        $json_arr = array('status' => 1, 'msg' => '操作完成', 'result' => '');
        $json_str = json_encode($json_arr);
        exit($json_str);
    }

    //发送测试邮件
    public function send_email() {
        $param = I('post.');
        tpCache($param['inc_type'], $param);
        if (send_email($param['test_eamil'], '后台测试', '测试发送验证码:' . mt_rand(1000, 9999))) {
            exit(json_encode(1));
        } else {
            exit(json_encode(0));
        }
    }

    /**
     *  管理员登录后 处理相关操作
     */
    public function login_task() {

        /*         * * 随机清空购物车的垃圾数据 */
        $time = time() - 3600; // 删除购物车数据  1小时以前的
        M("Cart")->where("user_id = 0 and  add_time < $time")->delete();
        $today_time = time();

        // 发货后满多少天自动收货确认
        $auto_confirm_date = tpCache('shopping.auto_confirm_date');
        $auto_confirm_date = $auto_confirm_date * (60 * 60 * 24); // 7天的时间戳
        $order_id_arr = M('order')->where("order_status = 1 and shipping_status = 1 and ($today_time - shipping_time) >  $auto_confirm_date")->getField('order_id', true);
        foreach ($order_id_arr as $k => $v) {
            confirm_order($v);
        }

        // 多少天后自动分销记录自动分成
        $switch = tpCache('distribut.switch');
        if ($switch == 1 && file_exists(APP_PATH . 'Common/Logic/DistributLogic.class.php')) {
            $distributLogic = new \Common\Logic\DistributLogic();
            $distributLogic->auto_confirm(); // 自动确认分成
        }
    }

    function ajax_get_action() {
        $control = I('controller');
        $advContrl = get_class_methods("Admin\\Controller\\" . $control);
        //dump($advContrl);
        $baseContrl = get_class_methods('Admin\Controller\BaseController');
        $diffArray = array_diff($advContrl, $baseContrl);
        $html = '';
        foreach ($diffArray as $val) {
            $html .= "<option value='" . $val . "'>" . $val . "</option>";
        }
        exit($html);
    }

    function right_list() {
        $group = array('system' => '系统设置', 'content' => '内容管理', 'goods' => '商品中心', 'member' => '会员中心',
            'order' => '订单中心', 'marketing' => '营销推广', 'tools' => '插件工具', 'count' => '统计报表'
        );
        $right_list = M('system_menu')->select();
        $this->assign('right_list', $right_list);
        $this->assign('group', $group);
        $this->display();
    }

    public function edit_right() {
        if (IS_POST) {
            $data = I('post.');
            $data['right'] = implode(',', $data['right']);
            if (!empty($data['id'])) {
                M('system_menu')->where(array('id' => $data['id']))->save($data);
            } else {
                if (M('system_menu')->where(array('name' => $data['name']))->count() > 0) {
                    $this->error('该权限名称已添加，请检查', U('System/right_list'));
                }
                unset($data['id']);
                M('system_menu')->add($data);
            }
            $this->success('操作成功', U('System/right_list'));
            exit;
        }
        $id = I('id');
        if ($id) {
            $info = M('system_menu')->where(array('id' => $id))->find();
            $info['right'] = explode(',', $info['right']);
            $this->assign('info', $info);
        }
        $group = array('system' => '系统设置', 'content' => '内容管理', 'goods' => '商品中心', 'member' => '会员中心',
            'order' => '订单中心', 'marketing' => '营销推广', 'tools' => '插件工具', 'count' => '统计报表'
        );
        $planPath = APP_PATH . 'Admin/Controller';
        $planList = array();
        $dirRes = opendir($planPath);
        while ($dir = readdir($dirRes)) {
            if (!in_array($dir, array('.', '..', '.svn'))) {
                $planList[] = basename($dir, '.class.php');
            }
        }
        $this->assign('planList', $planList);
        $this->assign('group', $group);
        $this->display();
    }

    public function right_del() {
        $id = I('del_id');
        if (is_array($id)) {
            $id = implode(',', $id);
        }
        if (!empty($id)) {
            $r = M('system_menu')->where("id in ($id)")->delete();
            if ($r) {
                respose(1);
            } else {
                respose('删除失败');
            }
        } else {
            respose('参数有误');
        }
    }

}
